﻿using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.Models;
using IPR_StudyMeetingMrgSys.ViewModel;
using log4net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Web;
using System.Web.Http;

namespace IPR_StudyMeetingMrgSys.Controllers
{
    public class TrainFileDownLogController : ApiController
    {
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        BLL.TrainFileDownLog BllTrainFileDownLog = new BLL.TrainFileDownLog();
        [HttpPost]
        [Route("api/TrainFileDownLog/Add")]
        public IHttpActionResult AddTrainFileDownLog(Model.TrainFileDownLog model)
        {
            model.DownDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            log.Info($"TrainFileDownLogID:{model.DownUserID};DownUserName:{model.DownUserName};DownDateTime:{model.DownDateTime}");

            ResultMsg resultMsg = new ResultMsg();
            model.DownUserName = LoginUser.UserInfo.UserName;
            if (BllTrainFileDownLog.Add(model) > 0)
            {
                //修改下载次数，更新+1
                string ErrorMsg = string.Empty;
                int iResult= new BLL.SysManage().ExecuteUpdate("TrainFiles", "TFDownCount", "ISNULL(TFDownCount,0)+1", "TFID", model.TFID.ToString(),out ErrorMsg);
                if (iResult > 0)
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = null;
                }
            }
            return Json(resultMsg);
        }
        [HttpGet]
        [Route("api/TrainFileDownLog/Add2")]
        public IHttpActionResult AddTrainFileDownLog2(string filename)
        {
            Model.TrainFileDownLog model = new Model.TrainFileDownLog();
            DataSet ds = new IPR_StudyMeetingMrgSys.BLL.TrainFiles().GetList(" TFName='" + filename.Split('.')[0] + "' ");
            if(ds!=null)
                if(ds.Tables.Count>0)
                    if(ds.Tables[0].Rows.Count>0)
                    {
                        model.TFID = Int32.Parse(ds.Tables[0].Rows[0]["TFID"].ToString());
                    }
            model.DownUserID = LoginUser.UserInfo.UserID;
            model.DownDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            log.Info($"TrainFileDownLogID:{model.DownUserID};DownUserName:{model.DownUserName};DownDateTime:{model.DownDateTime}");

            ResultMsg resultMsg = new ResultMsg();
            model.DownUserName = LoginUser.UserInfo.UserName;
            if(model.TFID>0)
            if (BllTrainFileDownLog.Add(model) > 0)
            {
                //修改下载次数，更新+1
                string ErrorMsg = string.Empty;
                int iResult = new BLL.SysManage().ExecuteUpdate("TrainFiles", "TFDownCount", "ISNULL(TFDownCount,0)+1", "TFID", model.TFID.ToString(), out ErrorMsg);
                if (iResult > 0)
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = null;
                }
            }
            return Json(resultMsg);
        }
        [HttpPost]
        [Route("api/TrainFileDownLog/Sel")]
        public IHttpActionResult GetTrainFileDownLogList()
        {
            log.Info($"获取部门列表");
            ResultMsg resultMsg = new ResultMsg();
            DataTable dt= BllTrainFileDownLog.GetAllList().Tables[0];
            if (dt.Rows.Count > 0)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = dt;
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "没有获取到部门信息";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }


        [HttpPost]
        [Route("api/TrainFileDownLog/GetTrainFileDownLogInfo")]
        public IHttpActionResult GetTrainFileDownLogInfo()
        {
            log.Info($"获取部门列表并形成树结构所需的json串");
            ResultMsg resultMsg = new ResultMsg();
            DataTable dt = BllTrainFileDownLog.GetAllList().Tables[0];
            if (dt.Rows.Count > 0)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = dt;
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "没有获取到部门信息";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }
        [HttpPost]
        [Route("api/TrainFileDownLog/Del")]
        public IHttpActionResult DelTrainFileDownLog(Model.TrainFileDownLog model)
        {
            log.Info($"删除部门信息");
            ResultMsg resultMsg = new ResultMsg();
            
            if (BllTrainFileDownLog.Delete(model.AutoID))
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = "";
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "删除部门信息失败";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }
        [HttpPost]
        [Route("api/TrainFileDownLog/Edit")]
        public IHttpActionResult UpdateTrainFileDownLog(Model.TrainFileDownLog model)
        {
            log.Info($"更新部门信息");
            ResultMsg resultMsg = new ResultMsg();
            Model.TrainFileDownLog MolTrainFileDownLog = new Model.TrainFileDownLog();
            MolTrainFileDownLog.TFID = model.TFID;
            MolTrainFileDownLog.DownUserID = model.DownUserID;
            MolTrainFileDownLog.DownUserName = model.DownUserName;
            if (BllTrainFileDownLog.Update(MolTrainFileDownLog))
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = "";
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "更新部门信息失败";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }
    }
}
